﻿namespace PlugNT.Common
{
    using System;
    using System.Configuration;
    using System.Net;
    using System.Net.Mail;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Web.Mail;

    [StructLayout(LayoutKind.Sequential, Size=1)]
    public struct Mail
    {
        public static string Send(string username, string password, string host, int port, bool ssl, string from, string fromname, string to, string title, string content)
        {
            StringBuilder sberr = new StringBuilder();
            SmtpClient client = new SmtpClient(host, port) {
                UseDefaultCredentials = false,
                Credentials = new NetworkCredential(username, password),
                DeliveryMethod = SmtpDeliveryMethod.Network
            };
            if (port != 0x19)
            {
                client.EnableSsl = ssl;
            }
            System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage {
                From = new MailAddress(from, fromname),
                Subject = title,
                SubjectEncoding = Encoding.UTF8,
                Body = content,
                BodyEncoding = Encoding.UTF8,
                IsBodyHtml = true,
                Priority = System.Net.Mail.MailPriority.High
            };
            string[] tos = to.Split(new char[] { ';' });
            foreach (string s in tos)
            {
                try
                {
                    message.To.Clear();
                    message.To.Add(new MailAddress(s));
                    client.Send(message);
                }
                catch (Exception ex)
                {
                    sberr.Append(ex.Message + "<br />");
                }
            }
            return sberr.ToString();
        }

        public static string Send(string username, string password, string host, string from, string to, string title, string content)
        {
            StringBuilder sberr = new StringBuilder();
            System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage();
            mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
            mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", username);
            mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", password);
            SmtpMail.SmtpServer = host;
            mail.From = from;
            mail.Subject = title;
            mail.BodyEncoding = Encoding.UTF8;
            mail.BodyFormat = MailFormat.Html;
            mail.Body = content;
            string[] tos = to.Split(new char[] { ';' });
            foreach (string s in tos)
            {
                try
                {
                    mail.To = s;
                    SmtpMail.Send(mail);
                }
                catch (Exception ex)
                {
                    sberr.Append(ex.Message + "<br />");
                }
            }
            return sberr.ToString();
        }

        public static bool SendMail(string To, string Subject, string Msg, Attachment attach)
        {
            string Email = ConfigurationSettings.AppSettings["Email"].ToString();
            string EmailPwd = ConfigurationSettings.AppSettings["EmailPwd"].ToString();
            string Smtp = ConfigurationSettings.AppSettings["Smtp"].ToString();
            System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage(Email, To, Subject, Msg) {
                BodyEncoding = Encoding.UTF8
            };
            if (attach != null)
            {
                mailMessage.Attachments.Add(attach);
            }
            mailMessage.IsBodyHtml = true;
            SmtpClient smtpClient = new SmtpClient {
                Host = Smtp,
                Port = 0x19
            };
            NetworkCredential o_Nc = new NetworkCredential(Email, EmailPwd);
            smtpClient.Credentials = o_Nc;
            try
            {
                smtpClient.Send(mailMessage);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static string JmailSend()
        {
            StringBuilder sberr = new StringBuilder();
            return sberr.ToString();
        }
    }
}

